package leetcode_day._2021._202108._1120;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/8/20 16:01
 * 反转字符串 II
 * 算法：双指针
 * abcdefg
 * bacdfeg
 * 快慢指针
 * 快指针先走 k 步，然后慢指针再走
 * 两个指针同时走，当走到 k 步时，反转慢指针的前 k 位，包括满指针位上的
 */
public class _20_541 {

    public String reverseStr(String s, int k) {
        final char[] array = s.toCharArray();
        for (int S = 0; S < array.length; S += 2 * k) {
            int L = S + k - 1;
            reverse(array, S, Math.min(L, array.length - 1));
        }
        return new String(array);
    }
    void reverse(char[] array, int start, int end) {
        while (start < end) {
            char tmp = array[start];
            array[start++] = array[end];
            array[end--] = tmp;
        }
    }

}
